package com.efast.dmom.mes.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.dmom.mes.bean.MbomItem;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.dao.entity.MbomItemDao;
import com.efast.dmom.mes.datahandlers.base.PartRelateDataHandler;
import com.efast.dmom.mes.entity.MbomItemEntity;
import com.efast.dmom.mes.util.BaseEntityDataUtil;

@Component
public class MbomItemDataHandler extends PartRelateDataHandler<MbomItem>{
	@Autowired
	private MbomItemDao mbomItemDao;
	
	@Override
	public ResultBean save(MbomItem obj) {
		ResultBean resultBean = new ResultBean();
		if(obj==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater is null");
			return resultBean;
		}
		MbomItemEntity entity = obj.getInnerData();
		if(entity==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater innerdata is null");
			return resultBean;
		}
		
		BaseEntityDataUtil.setBaseEntityData(entity);
		int save = mbomItemDao.save(entity);
		if(save>0) {
			resultBean.setSuccess(true);
		}else {
			resultBean.setSuccess(false);
			resultBean.setMessage("insert fail");
		}
		return resultBean;
	}

	@Override
	public ResultBean update(MbomItem obj) {
		ResultBean resultBean = new ResultBean();
		if(obj==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater is null");
			return resultBean;
		}
		MbomItemEntity entity = obj.getInnerData();
		if(entity==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater innerdata is null");
			return resultBean;
		}
		
		BaseEntityDataUtil.updateBaseEntityData(entity);
		int save = mbomItemDao.update(entity);
		if(save>0) {
			resultBean.setSuccess(true);
		}else {
			resultBean.setSuccess(false);
			resultBean.setMessage("insert fail");
		}
		return resultBean;
	}

	@Override
	public ResultBean delete(MbomItem obj) {
		ResultBean resultBean = new ResultBean();
		if(obj==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater is null");
			return resultBean;
		}
		MbomItemEntity entity = obj.getInnerData();
		if(entity==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater innerdata is null");
			return resultBean;
		}
		
		BaseEntityDataUtil.updateBaseEntityData(entity);
		int save = mbomItemDao.delete(entity);
		if(save>0) {
			resultBean.setSuccess(true);
		}else {
			resultBean.setSuccess(false);
			resultBean.setMessage("insert fail");
		}
		return resultBean;
	}

	@Override
	public ResultBean saveBatch(List<MbomItem> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean updateBatch(List<MbomItem> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean deleteBatch(List<MbomItem> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public MbomItem getById(long id) {
		MbomItemEntity mbomEntity = mbomItemDao.getById(id);
		if(mbomEntity!=null) {
			MbomItem mbomItem = new MbomItem();
			mbomItem.setInnerData(mbomEntity);
			return mbomItem;
		}
		return null;
	}
	
	public List<MbomItem> getByMbomId(long mbomId){
		List<MbomItemEntity> byMbomId = mbomItemDao.getByMbomId(mbomId);
		if(byMbomId!=null && byMbomId.size()>0) {
			List<MbomItem> list = new ArrayList<MbomItem>();
			for(MbomItemEntity entity:byMbomId) {
				MbomItem mbomItem = new MbomItem();
				mbomItem.setInnerData(entity);
				list.add(mbomItem);
			}
			return list;
		}
		return null;
	}
	
	public MbomItem getbyReleateId(long mbomId,long routeStepId,long mbomRsBomId) {
		MbomItemEntity mbomEntity = mbomItemDao.getbyReleateId(mbomId,routeStepId,mbomRsBomId);
		if(mbomEntity!=null) {
			MbomItem mbomItem = new MbomItem();
			mbomItem.setInnerData(mbomEntity);
			return mbomItem;
		}
		return null;
	}
}
